<?php

/**
 * @Author:      zwh
 * @DateTime:    2016-4-5 16:21:59
 * @Description: 用于 门店库存盘点相关的操作
 */
class ScsInventoryController extends BaseController
{
    /**
     * @Author:      zwh
     * @DateTime:    2016-4-5 16:22:37
     * @Description: 用于盘点的耗材记录
     */
    public function lists(){

    	$validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
    	do{
    		if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $msi=MaintenanceStorageItem::where('type',1)->where('status',1);
            if(Input::get('code')){
                $msi->where('code','like','%'.Input::get('code').'%');
            }
            if(Input::get('name')){
                $msi->where('name','like','%'.Input::get('name').'%');
            }
            if(Input::get('subject')){
                $msi->where('subject',Input::get('subject'));
            }
            $msi=$msi->get();

            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>sizeof($msi),"msi_info"=>$msi));
    	}while(false);
    	Log::info("info", array("system" => "scs" ,"event" => "lists", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-4-7 15:06:09
     * @Description: 添加盘点表
     */
    public function addStockTaking(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'msi_ids' => 'required',
                'mic_id' => 'required'
            )
        );
        $msi_ids=json_decode(Input::get('msi_ids'),1);
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $response=DB::transaction(function () use ($msi_ids) {
                foreach($msi_ids as $msi_id){
                    $st=StockTake::where('msi_id',$msi_id)->where('mic_id',Input::get('mic_id'))->first();
                    if($st){
                        $st->variance=Input::get('number');
                    }else{
                        $msi=MaintenanceStorageItem::find($msi_id);
                        $st=new StockTake();
                        $st->mci_id=Input::get('mic_id');
                        $st->msi_id=$msi_id;
                        $st->data_id=$msi->data_id;
                        $st->code=$msi->code;
                        $st->name=$msi->name;
                        $st->pack_unit=$msi->pack_unit;
                        $st->unit=$msi->unit;
                        $st->number=$msi->number;
                        $st->inventory_number=Input::get('number');
                        $st->variance_number=Input::get('number')-$msi->number;
                        $st->subject=$msi->subject;
                        $st->tax_purchase_price=$msi->tax_purchase_price;
                        $st->tax_purchase_subtotal=$msi->tax_purchase_subtotal;
                        $st->location=$msi->location;
                        $st->old_code=$msi->old_code;
                        $st->cust_id=$sc_user->id;
                        $st->save();
                    }
                }
                $arr=array();
                $arr[]=$st->id;
                return  array('code' => 0, 'msg' => 'ok', 'result' => $arr);
            });
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addStockTaking", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-4-5 16:22:37
     * @Description: 盘点盈亏记录列表
     */
    public function palList(){

        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
            );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $pal=ProfitAndLoss::where('status',1)->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $pal);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "lists", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-4-5 16:22:37
     * @Description: 删除盘点盈亏记录
     */
    public function noPalList(){

        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required'
            )
            );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $pal=ProfitAndLoss::where('status',2)->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $pal);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "deletePal", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
     /**
     * @Author:      zwh
     * @DateTime:    2016-4-7 15:06:09
     * @Description: 添加盈亏处理表
     */
    public function addProfitAndLoss(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'sts' => 'required'
            )
        );
        $sts=json_decode(Input::get('sts'));
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $response=DB::transaction(function () use ($st_ids) {
                foreach($sts as $st){
                    $stockTake=StockTake::find($st['id']);
                    $pal=new ProfitAndLoss();
                    $pal->st_id=$st['id'];
                    $pal->data_id=$stockTake->data_id;
                    $pal->code=$stockTake->code;
                    $pal->name=$stockTake->name;
                    $pal->pack_unit=$stockTake->pack_unit;
                    $pal->unit=$stockTake->unit;
                    $pal->number=$stockTake->number;
                    $pal->inventory_number=$stockTake->inventory_number;
                    $pal->variance_number=$stockTake->variance_number;
                    $pal->subject=$stockTake->subject;
                    $pal->tax_purchase_price=$stockTake->tax_purchase_price;
                    $pal->tax_purchase_subtotal=$stockTake->tax_purchase_subtotal;
                    $pal->location=$stockTake->location;
                    $pal->old_code=$msi->old_code;
                    $pal->cust_id=$sc_user->id;
                    $pal->status=1;
                    $pal->save();
                }
                $arr=array();
                $arr[]=$st->id;
                return  array('code' => 0, 'msg' => 'ok', 'result' => $arr);
            });
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addStockTaking", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      zwh
     * @DateTime:    2016-4-11 11:18:46
     * @Description: 删除盈亏处理表记录
     */
    public function deletePal(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $pal=ProfitAndLoss::find(Input::get('id'));
            $pal->status=2;
            $pal->save();
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "deletePal", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-4-11 10:52:13
     * @Description: 执行盘平处理
     */
    public function closeOut(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required'
            )
            );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $pal=ProfitAndLoss::where('status',1)->where('mic_id',Input::get('id'))->get();
            $response=ProfitAndLoss::implement($pal,$sc_user);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "closeOut", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}